if string.match(vim.o.rtp, "nvim%-notify") then
    vim.notify = require("notify")
end

local utils = {}

utils.find_git_root = function()
    local path = vim.fn.system({ "git", "rev-parse", "--show-toplevel" })
    path = string.sub(path, 1, -2)
    if string.find(path, "fatal") then
        return nil
    else
        return path
    end
end

utils.open_gitignore = function()
    local root = utils.find_git_root()
    if root ~= nil then
        vim.api.nvim_exec(":tabnew " .. root .. [[/.gitignore]], true)
    end
end

vim.api.nvim_create_user_command("Sit", function(opts)
    local root_path = utils.find_git_root()
    local current_dir = vim.fn.getcwd()
    if root_path ~= nil then
        vim.api.nvim_set_current_dir(root_path)
        if string.match(opts.args, "add%s+%%") then --  add current edited file
            vim.api.nvim_command("!git add " .. vim.fn.expand("%:p"))
            -- vim.api.nvim_exec("!git add " .. vim.fn.expand("%:p"), false)
            -- vim.fn.system({ "git", "add", vim.fn.expand("%:p") })
        else
            -- vim.fn.system("cd " .. root_path)
            vim.api.nvim_command("!git " .. opts.args)
            -- print(vim.fn.system("git " .. opts.args))
        end
        vim.api.nvim_set_current_dir(current_dir)
    end
end, { nargs = "+" })
vim.keymap.set("n", "<space>;", ":Sit<space>", { desc = "Git command" })

vim.api.nvim_create_user_command("Calc", function(opts)
    vim.api.nvim_command("!qalc " .. "'" .. opts.args .. "'")
end, { nargs = "+" })
vim.keymap.set("n", "<space>dc", ":Calc<space>", { desc = "calculate expression" })

vim.api.nvim_create_user_command("CalcLine", function()
    -- 获得当前行内容
    local line = vim.api.nvim_win_get_cursor(0)[1]
    local expression = vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]
    expression = string.gsub(expression, "%s+$", "") -- 删除尾部的空格
    local residue = expression
    expression = string.match(expression, "%w.*") -- 删除前面的非数字字符

    local result = vim.api.nvim_exec2(":!qalc " .. "'" .. expression .. "'", { output = true }).output
    -- print(result)

    if string.match(result, "错误") == nil then
        local lastLine = nil
        result:gsub("[^\r\n]+", function(line)
            lastLine = line
            lastLine = string.match(lastLine, ".*[=≈](.*)$") -- =, ≈后面的内容
        end)
        vim.api.nvim_buf_set_lines(0, line - 1, line, true, { residue .. " =" .. lastLine })
    else
        vim.notify(result, vim.log.levels.ERROR)
    end
end, { nargs = 0 })
vim.keymap.set("n", "<space>dl", ":CalcLine<cr>", { desc = "use qalc to calculate current line" })

utils.compile = function()
    if vim.opt.filetype:get() == "c" then
        vim.api.nvim_exec2("make", false)
        vim.api.nvim_command("!./a.out")
    elseif vim.opt.filetype:get() == "cpp" then
        vim.api.nvim_exec2("make", false)
        vim.api.nvim_command("!./a.out")
    elseif vim.opt.filetype:get() == "dot" then
        vim.api.nvim_exec("!dot -Tpng % -o " .. vim.fn.expand("%:r") .. ".png", false)
    elseif vim.opt.filetype:get() == "lua" then
        vim.api.nvim_command("luafile %")
    elseif vim.opt.filetype:get() == "python" then
        vim.api.nvim_command("!python %")
    elseif vim.opt.filetype:get() == "rust" then
        vim.api.nvim_command("!rustc %")
        vim.api.nvim_command("!./" .. vim.fn.expand("%:r"))
    elseif vim.opt.filetype:get() == "sh" then
        vim.api.nvim_command("!source %")
    elseif vim.opt.filetype:get() == "tcl" then
        vim.api.nvim_command("!tclsh %")
    elseif vim.opt.filetype:get() == "zig" then
        vim.api.nvim_command("!zig run %")
    else
        vim.notify("This filetype can't be compiled!", vim.log.levels.WARN)
    end
end
-- vim.keymap.set("n", "<space>dv", utils.compile, { desc = "compile files" })
vim.keymap.set("n", "<space>dv", "<cmd>lua require('utils').compile()<cr>", { desc = "compile files" })

vim.keymap.set("n", ",sg", function()
    local root = utils.find_git_root()
    if root ~= nil then
        vim.api.nvim_exec(":tabnew " .. root .. [[/.gitignore]], true)
    end
end, { desc = "open gitignore" })

return utils
